package battlecruiser.board.cell;

import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author tymoshya
 * @since 14.06.12
 */
public enum CellStatus {
    //Ship placement
    FREE("0"),
    SHIP("+"),
    BORDER_WATERS("."),
    //Shooting results
    MISSED("*"),
    DAMAGED("D"),
    DESTROYED("X")
    ;

    private static Map<String, CellStatus> stringCellStatusMap;
    private static Set<String> allowedValues;

    static {
        stringCellStatusMap = initStringCellStatusMap();
        allowedValues = initAllowedValues();
    }

    public static CellStatus fromString(String s) {
        Assert.isTrue(allowedValues.contains(s), "Can create CellStatus only from predefined string constants. Wrong: " + s);
        return stringCellStatusMap.get(s);
    }

    private static Map<String, CellStatus> initStringCellStatusMap() {
        Map<String, CellStatus> result = new HashMap<String, CellStatus>();
        result.put("0", FREE);
        result.put("+", SHIP);
        result.put(".", BORDER_WATERS);
        result.put("*", MISSED);
        result.put("D", DAMAGED);
        result.put("X", DESTROYED);
        return result;
    }

    private static Set<String> initAllowedValues() {
        Set<String> result = new HashSet<String>();
        for(CellStatus status : CellStatus.values()) {
            result.add(status.symbol);
        }
        return result;
    }

    private String symbol;

    CellStatus(String symbol) {
        this.symbol = symbol;
    }


    @Override
    public String toString() {
        return symbol;
    }
}
